package edu.bit.water.service;

import java.util.List;

import edu.bit.water.dto.DateUtils;
import org.hibernate.validator.cfg.context.ReturnValueConstraintMappingContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import edu.bit.water.dao.LabelEvaCmpWaterAnlDao;
import edu.bit.water.entity.LabelEvaCmpWaterTAndASum;
import edu.bit.water.util.FormulaIntermediateVariable;

/**
 * (水平衡)企业用水分析业务层
 * @author HJS
 *
 */
@Service
public class LabelEvaCmpWaterAnlService {

	@Autowired
	private LabelEvaCmpWaterAnlDao labelEvaCmpWaterAnlDao;

	/**
	 *
	 * @param labelEvaCmpWaterAnl 用水分析表标签表
	 * @return 成功返回1，失败返回0
	 */
	public int add(LabelEvaCmpWaterTAndASum labelEvaCmpWaterAnl) {
		return labelEvaCmpWaterAnlDao.add(labelEvaCmpWaterAnl);
	}
	/**
	 *
	 * @return 成功返回所有记录，失败返回null
	 */
	public List<LabelEvaCmpWaterTAndASum> select(){
		return labelEvaCmpWaterAnlDao.select();
	}
	/**
	 *
	 * @param idLists 用水分析表标签表id集合
	 * @return 成功返回1，失败返回0
	 */
	public int delete(List<Integer> idLists) {
		try {
			for(int id:idLists){
				labelEvaCmpWaterAnlDao.delete(id);
				//labelEvaCmpWaterAnlDao.
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return 0;
	}
	/**
	 *
	 * @param id 用水分析表标签表id
	 * @return 成功返回对应的company_id，失败返回0
	 */
	public int selectCompanyId(int id){
		return labelEvaCmpWaterAnlDao.selectCompanyId(id);
	}
	/**
	 *
	 * @param labelEvaCmpWaterAnl 用水分析表标签表
	 * @return 成功返回1，失败返回0
	 */
	public int update(LabelEvaCmpWaterTAndASum labelEvaCmpWaterAnl) {
		return labelEvaCmpWaterAnlDao.update(labelEvaCmpWaterAnl);
	}
	/**
	 *
	 * @param id 标签表id
	 * @return 中间变量
	 */
	public FormulaIntermediateVariable selectMiddleVariable(int id) {
		//公司id
		LabelEvaCmpWaterTAndASum labelEvaCmpWaterTAndASum = labelEvaCmpWaterAnlDao.selectMiddleVariable(id);
		FormulaIntermediateVariable formulaIntermediateVariable = null ;
		if(labelEvaCmpWaterTAndASum!=null){
			labelEvaCmpWaterTAndASum.setWater1Sum_string(labelEvaCmpWaterAnlDao.getWater1Sum(id));
			labelEvaCmpWaterTAndASum.setUnconWaterUse_string(labelEvaCmpWaterAnlDao.getUnconWaterUse(id));
			formulaIntermediateVariable = new FormulaIntermediateVariable(id,labelEvaCmpWaterTAndASum.getWater1Sum(),labelEvaCmpWaterTAndASum.getProductOutput(),
					labelEvaCmpWaterTAndASum.getDirCooWaterCircu(),labelEvaCmpWaterTAndASum.getDirCooWaterConsump(),
					labelEvaCmpWaterTAndASum.getSteamCondensateReturn(), labelEvaCmpWaterTAndASum.getSteamproduction(),
					labelEvaCmpWaterTAndASum.getLossWaterSuml(),labelEvaCmpWaterTAndASum.getNewWaterSuml(),
					labelEvaCmpWaterTAndASum.getWasteWaterDischarge(),labelEvaCmpWaterTAndASum.getTotalWasteWaterDischarge(),
					labelEvaCmpWaterTAndASum.getRepeatWaterSuml(),labelEvaCmpWaterTAndASum.getNewWaterSuml(),
					labelEvaCmpWaterTAndASum.getIndirCooWaterCircu(),labelEvaCmpWaterTAndASum.getIndirCooNewWater(),
					labelEvaCmpWaterTAndASum.getExternalDisplacement(),labelEvaCmpWaterTAndASum.getNewWaterSuml(),
					labelEvaCmpWaterTAndASum.getWasteWaterReuse(),labelEvaCmpWaterTAndASum.getWasteWaterProduced(),
					labelEvaCmpWaterTAndASum.getUnconWaterUse(),labelEvaCmpWaterTAndASum.getWater1Sum());
		}
		return formulaIntermediateVariable;
	}
	/**
	 *
	 * @param formulaIntermediateVariable 用于保存中间参数的实体类
	 * @return
	 */
	public int updateVariable(FormulaIntermediateVariable formulaIntermediateVariable) {
		LabelEvaCmpWaterTAndASum labelEvaCmpWaterTAndASum = labelEvaCmpWaterAnlDao.selectMiddleVariable(formulaIntermediateVariable.getId());
		if(labelEvaCmpWaterTAndASum == null)
			labelEvaCmpWaterTAndASum = new LabelEvaCmpWaterTAndASum();

		labelEvaCmpWaterTAndASum.setWater1Sum_string(labelEvaCmpWaterAnlDao.getWater1Sum(formulaIntermediateVariable.getId()));
		labelEvaCmpWaterTAndASum.setUnconWaterUse_string(labelEvaCmpWaterAnlDao.getUnconWaterUse(formulaIntermediateVariable.getId()));
		formulaIntermediateVariable.setFormula1Variable1_string(labelEvaCmpWaterAnlDao.getWater1Sum(formulaIntermediateVariable.getId()));
		formulaIntermediateVariable.setFormula10Variable1(labelEvaCmpWaterTAndASum.getUnconWaterUse());
		formulaIntermediateVariable.setFormula10Variable2(labelEvaCmpWaterTAndASum.getWater1Sum());




		labelEvaCmpWaterTAndASum.setCompany_id(formulaIntermediateVariable.getId());//这里设置的id是公司id
		//公式1结果及中间变量
		labelEvaCmpWaterTAndASum.setUnitProductWaterWithdrawal(formulaIntermediateVariable.formula1());
		labelEvaCmpWaterTAndASum.setProductOutput(formulaIntermediateVariable.getFormula1Variable2());
		//公式2结果及中间变量
		labelEvaCmpWaterTAndASum.setDirCooWaterCircuRate(formulaIntermediateVariable.formula2());
		labelEvaCmpWaterTAndASum.setDirCooWaterCircu(formulaIntermediateVariable.getFormula2Variable1());
		labelEvaCmpWaterTAndASum.setDirCooWaterConsump(formulaIntermediateVariable.getFormula2Variable2());
		//公式3结果及中间变量
		labelEvaCmpWaterTAndASum.setSteamCondensateReuseRate(formulaIntermediateVariable.formula3());
		labelEvaCmpWaterTAndASum.setSteamCondensateReturn(formulaIntermediateVariable.getFormula3Variable1());
		labelEvaCmpWaterTAndASum.setSteamproduction(formulaIntermediateVariable.getFormula3Variable2());
		labelEvaCmpWaterTAndASum.setSteamVolumeMass(1);
		//公式4结果及中间变量
		labelEvaCmpWaterTAndASum.setLossRate(formulaIntermediateVariable.formula4());
		//公式5结果及中间变量
		labelEvaCmpWaterTAndASum.setWasteWaterDischargeRate(formulaIntermediateVariable.formula5());
		labelEvaCmpWaterTAndASum.setWasteWaterDischarge(formulaIntermediateVariable.getFormula5Variable1());
		labelEvaCmpWaterTAndASum.setTotalWasteWaterDischarge(formulaIntermediateVariable.getFormula5Variable2());
		//公式6结果及中间变量
		labelEvaCmpWaterTAndASum.setRepeatUtilizationRate(formulaIntermediateVariable.formula6());
		//公式7结果及中间变量
		labelEvaCmpWaterTAndASum.setIndirCooWaterCircuRate(formulaIntermediateVariable.formula7());
		labelEvaCmpWaterTAndASum.setIndirCooWaterCircu(formulaIntermediateVariable.getFormula7Variable1());
		labelEvaCmpWaterTAndASum.setIndirCooNewWater(formulaIntermediateVariable.getFormula7Variable2());
		//公式8结果及中间变量
		labelEvaCmpWaterTAndASum.setDrainageRate(formulaIntermediateVariable.formula8());
		labelEvaCmpWaterTAndASum.setExternalDisplacement(formulaIntermediateVariable.getFormula8Variable1());
		//公式9结果及中间变量
		labelEvaCmpWaterTAndASum.setWasteWaterReuseRate(formulaIntermediateVariable.formula9());
		labelEvaCmpWaterTAndASum.setWasteWaterReuse(formulaIntermediateVariable.getFormula9Variable1());
		labelEvaCmpWaterTAndASum.setWasteWaterProduced(formulaIntermediateVariable.getFormula9Variable2());
		//公式10结果及中间变量
		labelEvaCmpWaterTAndASum.setUnconWaterReplaceRate(formulaIntermediateVariable.formula10());
		return labelEvaCmpWaterAnlDao.updateVariable(labelEvaCmpWaterTAndASum);
	}
	/**
	 *
	 *
	 * @param id
	 * @return 返回计算值和统计值
	 */
	public LabelEvaCmpWaterTAndASum selectSumResult(int id) {
		//公司id
		//1.先找出中间变量,并把中间结果放到实体里
		FormulaIntermediateVariable formulaIntermediateVariable = selectMiddleVariable(id);
		if (formulaIntermediateVariable != null){
//			if (formulaIntermediateVariable.divisor()) {
//				//2.再调用update方法，将结果值和变量重新写到数据库中
//				updateVariable(formulaIntermediateVariable);
//			}
			updateVariable(formulaIntermediateVariable);
			LabelEvaCmpWaterTAndASum result = labelEvaCmpWaterAnlDao.selectSumResult(id);
			//result.showContent();
			return result;
		}
		else
			return  null;
	}
}
